﻿using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using SoonCode.FocusTest.Events;
using SoonCode.FocusTest.Models;
using SoonCode.FocusTest.Utils;

namespace SoonCode.FocusTest.ViewModels
{
    internal class AnswerItemCViewModel:BindableBase
    {
        private string itemType = "itemC";
        private ObservableCollection<string> answerItems;
        private IEventAggregator aggregator;
        private EventInfo answerInfo;
        private int Level;
        private int currItem;
        private Dictionary<int,int> waitCount;
        private Visibility fiveAgeTip = Visibility.Visible;
        private Visibility twoAgeTip = Visibility.Collapsed;

        public Visibility TwoAgeTip
        {
            get { return twoAgeTip; }
            set
            {
                twoAgeTip = value;
                RaisePropertyChanged();
            }
        }
        public Visibility FiveAgeTip
        {
            get { return fiveAgeTip; }
            set
            {
                fiveAgeTip = value;
                RaisePropertyChanged();
            }
        }
        public ObservableCollection<String> AnswerItems
        {
            get { return answerItems; }
            set
            {
                answerItems=value;
                RaisePropertyChanged();
            }
        }

        public AnswerItemCViewModel(IEventAggregator aggregator)
        {
            this.aggregator = aggregator;
             this.answerInfo=new EventInfo();
             answerInfo.Type = itemType;
             waitCount=new Dictionary<int,int>();
             this.aggregator.GetEvent<LevelUpdateEvent>().Subscribe(info =>
             {
                 if (info.Type != itemType) return;
                 AnswerItems = null;
                 Level = (int)info.Data["Level"];
                 waitCount[Level]=0;
                 currItem = 0;
             });
             this.aggregator.GetEvent<QuestionChangeEvent>().Subscribe(info =>
             {
                 if (info.Type != itemType) return;
                 int level = (int)info.Data["Level"];
                 currItem= (int)info.Data["index"];
                 waitCount[level] = (int)info.Data["index"]+1;
             });
             this.aggregator.GetEvent<TestLoadEvent>().Subscribe(OnLoaded);

        }

        private void OnLoaded(int p)
        {
            if (p != 2) return;
            AnswerItems = new ObservableCollection<string>();
            waitCount.Clear();
            if (GlobalModel.Instance.MemberInfo.age >= 5)
            {   
                FiveAgeTip = Visibility.Visible;
                TwoAgeTip = Visibility.Collapsed;
            }
            else if (GlobalModel.Instance.MemberInfo.age < 5)
            {
                TwoAgeTip = Visibility.Visible;
                FiveAgeTip = Visibility.Collapsed;
            }
        }

        public ICommand AnswerCommand
        {
            get
            {
                return new DelegateCommand<string>((p) =>
                {   
                    SoundPlayer.PlayButton();
                    if(AnswerItems==null)AnswerItems=new ObservableCollection<string>();
                    if (waitCount.Count<=0||waitCount[Level] <= AnswerItems.Count) return;
                    AnswerItems.Add($"pack://application:,,,/Images/{p}.png");
                    //answerInfo.Data["index"] = currItem;
                    answerInfo.Data["index"] = AnswerItems.Count - 1;
                    answerInfo.Data["img"] = p;
                    this.aggregator.GetEvent<StudentAnswerEvent>().Publish(answerInfo);
                });

            }
        }

    }
}
